package camelinaction;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Scope("prototype")//有这个标签，是多例的处理方式
@Slf4j
@Component
public class MyDispatcherServlet extends DispatcherServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void onRefresh(ApplicationContext context) {
		initStrategies(context);
	}

	@Override
    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.info("请求调用----："+request.getRequestURL());
        super.doService(request, response);
    }
	
	public void initContext(ApplicationContext context) {
		onRefresh(context);
		
		try {
			ServletConfig config = new MockServletConfig();
			super.init(config);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
